package com.sogoodsoft.web.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.encode.JsonBinder;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.sogoodsoft.entity.app.Organization;
import com.sogoodsoft.service.app.OrganizationService;
import com.sogoodsoft.web.CrudActionSupport;

@Results( {
	@Result(name = "ORG", location = "/WEB-INF/content/sys/organization.jsp")
})
public class OrganizationUIAction extends CrudActionSupport<Organization> {



	private static final long serialVersionUID = -1560573312421237951L;

	private List<Organization> tree;
	
	private OrganizationService orgService;
	
	private Long id;
	private Organization entity;
	private String code;
	private String ul;
	private static JsonBinder binder = JsonBinder.buildNonDefaultBinder();
	public String getOrgByCode(){
		logger.debug("getOrgByCode");
		Map<String, Object> msg = new HashMap<String, Object>();
		String json;
		if(code!=null){
			entity = orgService.getByCode(code);
			
		}
		if(entity==null){
			msg.put("error", true);
			msg.put("msg", "未找到对应类型！");
			return null;
		}else{
		msg.put("typeName", entity.getName());
		msg.put("id", entity.getId());
		msg.put("error",false );
		msg.put("type", new Object());
		json = binder.toJson(msg);
		List<Organization> list = orgService.composite(entity);
		StringBuffer buffer = new StringBuffer();		
		for (Organization org : list) {
			buffer.append(",{\"parentId\":"+org.parentId+",\"name\":\""+org.getName()+"\",\"value\":\"0\",\"id\":"+org.getId()+"}");
		}
		String str = buffer.substring(1);
		str="["+str+"]";
		json = json.replace("{}", str);
		logger.debug(json);
		Struts2Utils.renderJson(json, "encoding:utf-8");	
		}
		return null;
	}
	
	/**
	 * 递归按级别生成UL
	 * @param org
	 * @return
	 */
	protected String ulComposite(Organization org){
		StringBuffer ul = new StringBuffer();
		if(org.hasChildren()){
			ul.append("<li><a href=\"#"+org.getId().longValue()+"\" class=\"orgTree\" >"+org.name+"</a>\n");
			ul.append("<ul>\n");
			List<Organization> list = org.getChildren();
			for(Organization o:list){
				ul.append(ulComposite(o));
			}
			ul.append("</ul>\n");
			ul.append("</li>\n");
		}else{
			ul.append("<li><a href=\"#"+org.getId().longValue()+"\">"+org.name+"</a></li>\n");
		}
		return ul.toString();
	}
	
	public String getOrgUL(){
		if(code==null){
			logger.error("组织结构获取错误，没有传递起始节点！");
		}
		entity = orgService.getByCode(code);
		ul = ulComposite(entity);
		return SUCCESS;
	}
	
	public String getAllOrgUL(){
		entity = orgService.getRoot();
		ul = ulComposite(entity);
		return "ORG";
	}
	
	
	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Organization getModel() {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Organization> getTree() {
		return tree;
	}

	public void setTree(List<Organization> tree) {
		this.tree = tree;
	}

	public OrganizationService getOrgService() {
		return orgService;
	}

	@Autowired
	public void setOrgService(OrganizationService orgService) {
		this.orgService = orgService;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Organization getEntity() {
		return entity;
	}

	public void setEntity(Organization entity) {
		this.entity = entity;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}

	public String getUl() {
		return ul;
	}

	public void setUl(String ul) {
		this.ul = ul;
	}

	
}
